feat(codegen): Add EmitC tile debug print support - #994
Conversation
646d4a3 to
0afdbd3
Compare
| func.walk([&](Operation *op) { | ||
| if (isa<pto::TReshapeOp>(op)) | ||
| // TReshapeOp (zero-copy alias) and TPrintOp (cce::printf side effect) are | ||
| // lowered natively by PTOToEmitC (TRESHAPE / TPRINT) with no vector |
There was a problem hiding this comment.
tprint只能在emitc后端用吗?vpto后端的实现方案是什么?
There was a problem hiding this comment.
目前 tprint 只支持 EmitC。EmitC 已有 PTOPrintToTPRINT lowering,能直接生成 pto-isa TPRINT 调用;VPTO 侧目前没有 TPrintOp/TPRINT 的 emission/runtime debug print 实现。因此这版不做 TileLib 模板展开,并在 VPTO backend下遇到 pto.tprint 时显式报 unsupported,避免静默走错路径。后续 VPTO 支持需要先补 VPTO lowering/runtime 的 print side-effect 方案。
2c3b929 to
79e7d4c
Compare
7297be4 to
7899014
Compare
355e92d to
36e131e
Compare
d5ec558 to
5e4558a
Compare
| static bool isCIdentChar(char c) { | ||
| return std::isalnum(static_cast<unsigned char>(c)) || c == '_'; | ||
| } | ||
|
|
||
| static std::string makeVPTOABIShimName(llvm::StringRef abiName) { | ||
| std::string shimName; | ||
| shimName.reserve(abiName.size() + strlen("__ptoas_")); | ||
| for (char c : abiName) { | ||
| if (c == '.') { | ||
| shimName.append("__ptoas_"); | ||
| continue; | ||
| } | ||
| shimName.push_back(c); | ||
| } | ||
| return shimName; | ||
| } | ||
|
|
||
| static std::optional<std::pair<std::string, std::string>> | ||
| rewriteVPTOABIDeclarationLine(std::string &line) { | ||
| if (line.find("extern \"C\"") == std::string::npos) | ||
| return std::nullopt; | ||
|
|
||
| size_t suffixPos = line.find(".vector("); | ||
| size_t suffixLen = strlen(".vector"); | ||
| if (suffixPos == std::string::npos) { | ||
| suffixPos = line.find(".cube("); | ||
| suffixLen = strlen(".cube"); | ||
| } | ||
| if (suffixPos == std::string::npos) | ||
| return std::nullopt; | ||
|
|
||
| size_t semiPos = line.find(';', suffixPos); | ||
| if (semiPos == std::string::npos || line.find('{', suffixPos) != std::string::npos) | ||
| return std::nullopt; | ||
|
|
||
| size_t nameStart = suffixPos; | ||
| while (nameStart > 0 && isCIdentChar(line[nameStart - 1])) | ||
| --nameStart; | ||
| if (nameStart == suffixPos) | ||
| return std::nullopt; | ||
|
|
||
| const size_t nameLen = suffixPos + suffixLen - nameStart; | ||
| std::string abiName = line.substr(nameStart, nameLen); | ||
| std::string shimName = makeVPTOABIShimName(abiName); | ||
| line.replace(nameStart, nameLen, shimName); | ||
| semiPos = line.find(';', nameStart + shimName.size()); | ||
| line.insert(semiPos, " __asm__(\"" + abiName + "\")"); | ||
| return std::make_pair(std::move(abiName), std::move(shimName)); | ||
| } | ||
|
|
||
| static void replaceAllTokenCalls(std::string &text, llvm::StringRef from, | ||
| llvm::StringRef to) { | ||
| std::string needle = (from + "(").str(); | ||
| std::string replacement = (to + "(").str(); | ||
| size_t pos = 0; | ||
| while ((pos = text.find(needle, pos)) != std::string::npos) { | ||
| text.replace(pos, needle.size(), replacement); | ||
| pos += replacement.size(); | ||
| } | ||
| } | ||
|
|
||
| static void rewriteVPTOABIDirectCallShims(std::string &cpp) { | ||
| llvm::StringMap<std::string> shimByABIName; | ||
| std::string rewritten; | ||
| rewritten.reserve(cpp.size()); | ||
|
|
||
| size_t lineStart = 0; | ||
| while (lineStart < cpp.size()) { | ||
| size_t lineEnd = cpp.find('\n', lineStart); | ||
| bool hasNewline = lineEnd != std::string::npos; | ||
| if (!hasNewline) | ||
| lineEnd = cpp.size(); | ||
|
|
||
| std::string line = cpp.substr(lineStart, lineEnd - lineStart); | ||
| if (auto mapping = rewriteVPTOABIDeclarationLine(line)) | ||
| shimByABIName[mapping->first] = mapping->second; | ||
|
|
||
| rewritten.append(line); | ||
| if (hasNewline) | ||
| rewritten.push_back('\n'); | ||
| lineStart = lineEnd + (hasNewline ? 1 : 0); | ||
| } | ||
|
|
||
| for (const auto &entry : shimByABIName) | ||
| replaceAllTokenCalls(rewritten, entry.getKey(), entry.getValue()); | ||
| cpp = std::move(rewritten); | ||
| } | ||
|
|
There was a problem hiding this comment.
这段代码是在 EmitC 生成 C++ 后,把非法的 VPTO ABI 符号名如 external_vadd.vector / external_vadd.cube 改写成合法 C++ shim 名,比如 external_vadd__ptoas_vector,同时用 asm("external_vadd.vector") 保留真实链接符号。
作用:让 C++ 能编译通过,并且 fatobj link 仍然能链接到 VPTO child 导出的 .vector/.cube ABI 符号。解决的是 mixed-backend direct call 下的编译/链接失败。
80718df to
d542ad6
Compare
|
Warning: @Crystal-wzy, ci-sim exceeded its soft runtime budget.
This warning is advisory only and does not affect required checks. Please inspect the step timings for an unexpected regression. |
Created-by: zhywang Commit-by: zhywang Merged-by: cann-robot Description: presmoke A3 simple 流程按设备分片并发执行 ST case,并为 gen_data 使用进程级临时脚本避免并发冲突。 ## 描述 2 files changed, 290 insertions(+), 201 deletions(-) - **范围**:涉及 `tests/run_st.sh` 的 A3 simple presmoke 执行调度,以及 `tests/script/run_st.py` 的 gen_data 生成流程。 - **问题**:A3 simple presmoke case 串行执行耗时较长;并发运行时多个进程会复用 `build/gen_data.py`,存在互相覆盖和清理冲突风险。 - **修复**:新增 `run_a3_simple_parallel` 和 `run_a3_simple_shard`,根据 `A3_SIMPLE_VISIBLE_DEVICES`/`ASCEND_RT_VISIBLE_DEVICES` 与 `A3_SIMPLE_PARALLEL` 将 case 分片到多个设备并发执行;`run_gen_data` 改为使用带 PID 的 `gen_data_<pid>.py` 临时文件并在结束后清理。 - **影响**:A3 simple presmoke 默认可利用多个可见设备并发执行;gen_data 临时文件名变为进程隔离,减少并发冲突,无额外兼容性影响。 ## 关联的Issue ## 测试 - 未运行 - 结果:未运行;本次仅根据 `git status --short` 和 `git diff` 分析当前工作区修改并生成提交信息。 ## 文档更新 无。 ## 类型标签 - [ ] Bug修复 - [ ] 新特性 - [x] 性能优化 - [ ] 文档更新 - [ ] 其他,请描述:
Summary
pto.tile.printandPrintFormatbindings so PTODSL can emitpto.tprintwith optional scratch views and format attributespto.tprintthrough the EmitC backend with the required TPrintheaders,
asc_printfworkaround, and sync-pipe handling while rejectingunsupported VPTO lowering
ptoas --fatobjsupport for EmitC native builds, includingsingle-child EmitC containers and mixed-backend VPTO ABI call shims
formats, backend restrictions, fatobj validation, and simulator stdout
Testing
git diff --cached --checkpython -m py_compile ptodsl/ptodsl/_runtime/native_build.py ptodsl/tests/test_jit_compile.py test/tilelib-st/test_tilelib_st.py test/tilelib-st/a5/tprint/case.py